package net.wangds.imagecomp;

import junit.framework.TestCase;
import net.wangds.common.base.space.Loc;
import net.wangds.common.base.space.Size;
import net.wangds.imagecomp.comp.BufferedImageComp;
import net.wangds.imagecomp.comp.ImageClipComp;
import net.wangds.imagecomp.comp.ImageCombineComp;
import net.wangds.imagecomp.helper.ImageIoHelper;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class ImageCompTestCase {

    @Test
    public void testCombine(){
        try(InputStream bgis = new ClassPathResource("bg.png").getInputStream();
            InputStream iconis = new ClassPathResource("icon.png").getInputStream()){

            BufferedImage imgbg = ImageIoHelper.loadImageFromInputStream(bgis);
            BufferedImage imgicon = ImageIoHelper.loadImageFromInputStream(iconis);

            ImageCombineComp.combine(imgbg, imgicon, Loc.of(0,0), Size.of(50,50));

            ImageIO.write(imgbg, "png", new File(System.getProperty("user.home")+File.separator+"imgcombineout.png"));
            TestCase.assertTrue(new File(System.getProperty("user.home")+File.separator+"imgcombineout.png").delete());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testClip(){
        try(InputStream bgis = new ClassPathResource("bg.png").getInputStream()
           ){

            BufferedImage imgbg = ImageIoHelper.loadImageFromInputStream(bgis);


            imgbg = ImageClipComp.clipCircle(imgbg);

            ImageIO.write(imgbg, "png", new File(System.getProperty("user.home")+File.separator+"imgclip.png"));
            //TestCase.assertTrue(new File(System.getProperty("user.home")+File.separator+"imgclip.png").delete());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testCutSquare(){
        try(InputStream bgis = new ClassPathResource("bg.png").getInputStream()
        ){

            BufferedImage imgbg = ImageIoHelper.loadImageFromInputStream(bgis);

            imgbg = BufferedImageComp.cutSquare(imgbg);

            ImageIO.write(imgbg, "png", new File(System.getProperty("user.home")+File.separator+"cutsquare.png"));
            TestCase.assertTrue(new File(System.getProperty("user.home")+File.separator+"cutsquare.png").delete());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testScaleSquare(){
        try(InputStream bgis = new ClassPathResource("bg.png").getInputStream()
        ){

            BufferedImage imgbg = ImageIoHelper.loadImageFromInputStream(bgis);

            imgbg = BufferedImageComp.scale2Square(imgbg);

            ImageIO.write(imgbg, "png", new File(System.getProperty("user.home")+File.separator+"scalesquare.png"));
            TestCase.assertTrue(new File(System.getProperty("user.home")+File.separator+"scalesquare.png").delete());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
